package com.hbnu.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.mapper.IdentifyprojectMapper;
import com.hbnu.system.model.entity.Identify;
import com.hbnu.system.model.entity.Identifyproject;
import com.hbnu.system.model.entity.Porproject_registration;
import com.hbnu.system.model.entity.Tranproject_registration;
import com.hbnu.system.service.IIdentifyService;
import com.hbnu.system.service.IIdentifyprojectService;
import com.hbnu.system.service.IPorproject_registrationService;
import com.hbnu.system.service.ITranproject_registrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2023-05-16
 */
@Service
public class IdentifyprojectServiceImpl extends ServiceImpl<IdentifyprojectMapper, Identifyproject> implements IIdentifyprojectService {
//    @Autowired
//    IPorproject_midinspectionService iPorproject_midinspectionService;

    @Autowired
    ITranproject_registrationService iTranproject_registrationService;

    @Autowired
    IPorproject_registrationService iPorproject_registrationService;

    @Autowired
    IIdentifyService iIdentifyService;
    @Override
    public boolean addIdentifyProject(Identifyproject identifyproject) {
        //署名单位和
        if(StrUtil.isEmpty(identifyproject.getProjectName()) || StrUtil.isBlank(identifyproject.getProjectName())){
            throw new BaseException("请输入项目名称!");
        }
        Porproject_registration porproject = iPorproject_registrationService.getOne(Wrappers.lambdaQuery(Porproject_registration.class)
                        .eq(Porproject_registration::getProjectName, identifyproject.getProjectName())
        );

        Tranproject_registration tranproject = iTranproject_registrationService.getOne(Wrappers.lambdaQuery(Tranproject_registration.class)
                        .eq(Tranproject_registration::getProjectName, identifyproject.getProjectName())
        );

        if(Objects.isNull(porproject) && Objects.isNull(tranproject)){
            throw new BaseException("不存在该项目!");
        }

        if(Objects.nonNull(porproject)){
            identifyproject.setStartTime(porproject.getStartTime());
            identifyproject.setEndTime(porproject.getEndTime());
            identifyproject.setProjectID(porproject.getProjectId());
            identifyproject.setHead(porproject.getHead());
        }else {
            identifyproject.setStartTime(tranproject.getStartTime());
            identifyproject.setEndTime(tranproject.getEndTime());
            identifyproject.setProjectID(tranproject.getProjectId());
            identifyproject.setHead(tranproject.getHead());
        }

        //把对应的论文identify上的字段“关联项目”也要进行修改
        Identify identify = iIdentifyService.getOne(Wrappers.lambdaQuery(Identify.class)
                .eq(Identify::getIdentifyID, identifyproject.getIdentifyID()));
        if (Objects.isNull(identify)){
            throw new BaseException("成果鉴定不存在!");
        }


        //得到原来的联合项目名单
        String associated_projects = identify.getAssociatedProjects();
        if(identify.getAssociatedProjects().contains(identifyproject.getProjectName())){
            throw new BaseException("不可重复添加!");
        }
        if(associated_projects.isEmpty() || associated_projects.equals(" ")){
            identify.setAssociatedProjects(identifyproject.getProjectName());
        }else {
            identify.setAssociatedProjects(associated_projects + "、" +identifyproject.getProjectName());
        }
        //把相应identify进行修改使用update接口 更新它的相关项目这一字段
        UpdateWrapper<Identify> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("IdentifyID",identify.getIdentifyID());
        iIdentifyService.update(identify,updateWrapper);

        return baseMapper.insert(identifyproject) == 1;
    }


    @Override
    public boolean updateIdentifyProject(Identifyproject identifyproject) {
        return baseMapper.updateById(identifyproject) == 1;
    }

    @Override
    public boolean removeIdentifyProject(String IdentifyProjectID) {
        Identifyproject identifyproject = super.getOne(Wrappers.lambdaQuery(Identifyproject.class)
                .eq(Identifyproject::getIdentifyProjectID, IdentifyProjectID));
        if(Objects.isNull(identifyproject)){
            throw new BaseException("不存在该关联项目");
        }

        Identify identify = iIdentifyService.getOne(Wrappers.lambdaQuery(Identify.class)
                .eq(Identify::getIdentifyID, identifyproject.getIdentifyID()));
        //相关联的Identify里面的字段也要进行修改 此处先把要删除的项目名称提取出来
        String project_name = identifyproject.getProjectName();
        //此处把Identify里的相关项目的字段值提取出来
        String associated_projects = identify.getAssociatedProjects();
        //把项目名替换成空白即可 分三种情况 分别是顿号在前在后和不存在顿号
        String front = "、" + project_name;
        String behind = project_name + "、";
        String both = "、" + project_name + "、";
        if(associated_projects.contains(both)){
            String replace = associated_projects.replace(both, "、");
            identify.setAssociatedProjects(replace);
        }
        else if(associated_projects.contains(front) && !associated_projects.contains(behind)){
            String replace = associated_projects.replace(front, "");
            identify.setAssociatedProjects(replace);
        }
        else if (associated_projects.contains(behind) && !associated_projects.contains(front)){
            String replace = associated_projects.replace(behind, "");
            identify.setAssociatedProjects(replace);
        }
        else if(associated_projects.contains(project_name)){
            String replace = associated_projects.replace(project_name,"");
            identify.setAssociatedProjects(replace);
        }

        //对Identify进行更新
        UpdateWrapper<Identify> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("IdentifyID",identify.getIdentifyID());
        iIdentifyService.update(identify,updateWrapper);

        return baseMapper.deleteById(identifyproject) == 1;
    }

    @Override
    public List<Identifyproject> getIdentifyProjectByIdentifyID(String identifyID) {
        Identify byId = iIdentifyService.getById(identifyID);
        if (Objects.isNull(byId)){
            throw new BaseException("输入的成果鉴定编号有误!");
        }
        List<Identifyproject> identifyprojects = baseMapper.selectList(Wrappers.lambdaQuery(Identifyproject.class)
                .eq(Identifyproject::getIdentifyID, identifyID));
        return identifyprojects;
    }
}
